﻿using Infra.Tools.Logging;
using System;
using System.Collections.Generic;

namespace BLL.Service.Basis
{
    public abstract class UnitOfWork : IUnitOfWork
    {
        protected readonly IRepositoryFactory _factory;
        protected readonly ILogger _logger;
        private List<ITransactional> _container;

        protected UnitOfWork(IRepositoryFactory factory, ILogger logger)
        {
            this._factory = factory;
            this._logger = logger;
        }

        protected List<ITransactional> Container 
        {
            get
            {
                if (this._container == null)
                    this._container = new List<ITransactional>();

                return this._container;
            }
        }

        public void SaveChanges()
        {
            if (this._container != null)
            {
                foreach (var item in this._container)
                    item.SaveChanges();
            }
        }

        public void Dispose()
        {
            if (this._container != null)
            {
                foreach (var item in this._container)
                    item.Dispose();
            }
        }
    }
}
